import 'package:dio/dio.dart';
import 'package:fwanandroid/model/api/DioClient.dart';
import 'package:fwanandroid/model/bean/ArticleList.dart';
import 'package:fwanandroid/model/bean/BannerInfo.dart';
import 'package:fwanandroid/model/bean/Hot.dart';
import 'package:fwanandroid/model/bean/Navigation.dart';
import 'package:fwanandroid/model/bean/SystemParent.dart';
import 'package:fwanandroid/model/bean/User.dart';
import 'package:fwanandroid/model/api/exceptions/HttpException.dart';

import 'exceptions/ApiException.dart';

class WanAndroidService {
  static Future<User> login(String userName, String passWord) async {
    try {
      var apiResponse = await DioClient().post(
          '/user/login',
          FormData.fromMap({"username": userName, "password": passWord}),
          null,
          null);
      return User.fromJson(apiResponse.data);
    } catch (error) {
      throw handleException(error);
    }
  }

  static Future<void> logout() async {
    try {
      var res = await DioClient().get('/user/logout/json', null, null, null);
    } catch (error) {
      throw handleException(error);
    }
  }

  static Future<List<BannerInfo>> getBanners() async {
    try {
      var res = await DioClient().get('/banner/json', null, null, null);
      List<BannerInfo> banners = [];
      for (var item in res.data) {
        banners.add(BannerInfo.fromJson(item));
      }
      return banners;
    } catch (error) {
      throw handleException(error);
    }
  }

  static Future<ArticleList> getArticles(int page) async {
    try {
      var res =
          await DioClient().get('/article/list/${page}/json', null, null, null);
      return ArticleList.fromJson(res.data);
    } catch (error) {
      throw handleException(error);
    }
  }

  static Future<ArticleList> getSquareArticles(int page) async {
    try {
      var res = await DioClient()
          .get('/user_article/list/${page}/json', null, null, null);
      return ArticleList.fromJson(res.data);
    } catch (error) {
      throw handleException(error);
    }
  }

  static Future<List<SystemParent>> getSystemTypes() async {
    try {
      var res = await DioClient().get('/tree/json', null, null, null);
      var list = <SystemParent>[];
      for (var item in res.data) {
        list.add(SystemParent.fromJson(item));
      }
      return list;
    } catch (error) {
      throw handleException(error);
    }
  }

  static Future<ArticleList> getListProjectArticles(int page) async {
    try {
      var res = await DioClient()
          .get('/article/listproject/${page}/json', null, null, null);
      return ArticleList.fromJson(res.data);
    } catch (error) {
      throw handleException(error);
    }
  }

  static Future<List<Navigation>> getNavigation() async {
    try {
      var res = await DioClient().get('/navi/json', null, null, null);
      var list = <Navigation>[];
      for (var item in res.data) {
        list.add(Navigation.fromJson(item));
      }
      return list;
    } catch (error) {
      throw handleException(error);
    }
  }

  static Future<List<SystemParent>> getBlogType() async {
    try {
      var res =
          await DioClient().get('/wxarticle/chapters/json', null, null, null);
      var list = <SystemParent>[];
      for (var item in res.data) {
        list.add(SystemParent.fromJson(item));
      }
      return list;
    } catch (error) {
      throw handleException(error);
    }
  }

  static Future<ArticleList> getSystemTypeDetail(int page, int cid) async {
    try {
      var res = await DioClient()
          .get('/article/list/${page}/json', {"cid": cid}, null, null);
      return ArticleList.fromJson(res.data);
    } catch (error) {
      throw handleException(error);
    }
  }

  static Future<List<SystemParent>> getProjectType() async {
    try {
      var res = await DioClient().get('/project/tree/json', null, null, null);
      var list = <SystemParent>[];
      for (var item in res.data) {
        list.add(SystemParent.fromJson(item));
      }
      return list;
    } catch (error) {
      throw handleException(error);
    }
  }

  static Future<ArticleList> getProjectTypeDetail(int page, int cid) async {
    try {
      var res = await DioClient()
          .get('/article/list/${page}/json', {"cid": cid}, null, null);
      return ArticleList.fromJson(res.data);
    } catch (error) {
      throw handleException(error);
    }
  }

  static Future<List<Hot>> getHot() async {
    try {
      var res = await DioClient().get('/hotkey/json', null, null, null);
      var list = <Hot>[];
      for (var item in res.data) {
        list.add(Hot.fromJson(item));
      }
      return list;
    } catch (error) {
      throw handleException(error);
    }
  }

  static Future<List<Hot>> getWebsites() async {
    try {
      var res = await DioClient().get('/friend/json', null, null, null);
      var list = <Hot>[];
      for (var item in res.data) {
        list.add(Hot.fromJson(item));
      }
      return list;
    } catch (error) {
      throw handleException(error);
    }
  }

  static Future<ArticleList> searchHot(int page, String key) async {
    try {
      FormData formData = FormData.fromMap({"k":key});
      var res = await DioClient()
          .post('/article/query/${page}/json', formData, null, null);
      return ArticleList.fromJson(res.data);
    } catch (error) {
      throw handleException(error);
    }
  }

  static Exception handleException(error) {
    print(error);
    if (error is DioException &&
        (error as DioException).error is HttpException) {
      throw error.error!!;
    }
    throw ApiException(code: -1, message: "unknow error");
  }
}
